package com.alexander.cloundexplorer.dialog;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

import com.alexander.cloundexplorer.R;
import com.alexander.cloundexplorer.activity.RenameActivity;
import com.alexander.cloundexplorer.bean.DirectoryInfo;
import com.alexander.cloundexplorer.bean.params.DelDirectoryParams;
import com.alexander.cloundexplorer.bean.params.TrashFileParams;
import com.alexander.cloundexplorer.bean.params.User;
import com.alexander.cloundexplorer.bean.response.FileInfoResponse;
import com.alexander.cloundexplorer.codec.binary.StringUtils;
import com.alexander.cloundexplorer.util.Constants;
import com.alibaba.fastjson.JSON;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 *
 * @author Administrator
 * @date 2018/3/9
 */

public class DirectoryOptionsDialog extends BaseDialog
{
    private DirectoryInfo directoryInfo;
    private int layoutId = R.layout.dialog_diroptions_layout;
    private DelDirectoryParams delDirectoryParams = new DelDirectoryParams();
    private OnDelLisnter onDelLisnter;

    public DirectoryOptionsDialog(@NonNull Context context1, DirectoryInfo directoryInfo, User user1)
    {
        super(context1, user1);
        this.directoryInfo = directoryInfo;
    }

    public DirectoryOptionsDialog(@NonNull Context context1, int themeResId,  DirectoryInfo directoryInfo, User user1)
    {
        super(context1, themeResId, user1);
        this.directoryInfo = directoryInfo;
    }

    public DirectoryOptionsDialog(@NonNull Context context1, boolean cancelable, @Nullable OnCancelListener cancelListener, DirectoryInfo directoryInfo, User user1)
    {
        super(context1, cancelable, cancelListener, user1);
        this.directoryInfo = directoryInfo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(layoutId);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.reName_TextView, R.id.del_TextView})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.reName_TextView:
                Intent intent = new Intent(getContext(), RenameActivity.class);
                intent.putExtra("isFile", "0");
                intent.putExtra("info", directoryInfo);
                intent.putExtra("user", user);
                getContext().startActivity(intent);
                dismiss();
                break;
            case R.id.del_TextView:
                delDirectoryParams.setAccount(user.getPhone());
                delDirectoryParams.setPassword(user.getPassword());
                delDirectoryParams.setDirectoryId(directoryInfo.getId().toString());
                String jsonStr = JSON.toJSONString(delDirectoryParams);
                OkHttpUtils
                        .post()
                        .url(Constants.IP.concat("/fileController/delDirectory"))
                        .addParams("params", jsonStr)
                        .build()
                        .execute(new StringCallback()
                        {
                            @Override
                            public void onError(Call call, Exception e, int id)
                            {
                                showTip(e.getMessage());
                            }
                            @Override
                            public void onResponse(String response, int id)
                            {
                                showTip(response);
                                if(response.contains("1300"))
                                {}
                                else if(response.contains("1301"))
                                {}
                            }
                            @Override
                            public void onAfter(int id)
                            {
                                super.onAfter(id);
                                dismiss();
                            }
                        });
                break;
            default:
                break;
        }
    }

    /**
     * 设置列表项点击监听器
     * */
    public void setOnDelLisnter(OnDelLisnter onDelLisnter)
    {
        this.onDelLisnter = onDelLisnter;
    }
    public interface OnDelLisnter
    {
        /**
         * 删除成功的会掉
         * @param result 返回值
         * @param fileInfoResponse 被删除的文件的对象
         * */
        void onDelSuccess(String result, FileInfoResponse fileInfoResponse);
        /**
         * 删除失败的回调
         * @param throwable 错误
         * */
        void onDelError(Throwable throwable);
    }
}
